<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :search.sync="search"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true">导出</el-button>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button type="text" icon="el-icon-view" size="small" @click.stop="handleView(row)">详情</el-button>
      </template>
    </avue-crud>
    <el-dialog title="详情" :visible.sync="showDetailDialog" width="60%" append-to-body="true">
      <div class="sail-header-dialog-content">
        <div class="formClass">
          <div class="title-result">
            <div class="title">
              <i class="el-icon-vertical-bar avue-group__icon"></i>
              <h3>基础数据</h3>
            </div>
          </div>
        </div>
        <avue-form ref="customerRef" :option="baseOption" v-model="form"> </avue-form>
        <div class="formClass">
          <div class="title-result">
            <div class="title">
              <i class="el-icon-vertical-bar avue-group__icon"></i>
              <h3>出库明细</h3>
            </div>
          </div>
        </div>
        <avue-crud
          ref="detailCrud"
          :option="detailOption"
          :table-loading="detailLoading"
          :page.sync="detailPage"
          :data="detailData"
          @current-change="detailCurrentChange"
          @size-change="detailSizeChange"
        >
        </avue-crud>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDetailDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
    <common-modal-template ref="commonModalTemplate" :option="tableOption" @getSelectedRows="getSelectedRows" :key="timer" @newKey="newKey"> </common-modal-template>
    <export-comp ref="exportComp" page-api-url="mes-warehouse/finished-outbound-record/page" :option="option" :page="page" :query="query" file-name="销售发货记录">
    </export-comp>
  </basic-container>
</template>

<script>
import { getList, getDetailList } from '@/api/report/finished-goods-shipment-query';
import { mapGetters } from 'vuex';
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';
import userTableConfig from '@/components/user-table-config';
import exportComp from '@/components/export-comp';
import userConfig from '@/mixins/userConfig';
export default {
  name: 'ReportFinishedGoodsShipmentQuery',
  components: {
    CommonModalTemplate,
    exportComp,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'FINSH_GOODS_SHIPMENT_QUERY',
      timer: new Date().getTime(), //不同内容使用同一个模态框结构，key值不同需要重新请求渲染数据，否则会请求到上一个使用的数据内容
      modalName: '', //打开公用模态框名称
      tableOption: {}, //公用模态框配置信息
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      search: {},
      selectionList: [],
      option: {
        height: 'calc(100vh - 580px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 6,
        searchMenuSpan: 24,
        labelWidth: 100,
        border: true,
        index: false,
        editBtn: false,
        viewBtn: false,
        selection: true,
        delBtn: false,
        addBtn: false,
        dialogClickModal: false,
        indexLabel: '序号',
        searchMenuPosition: 'right',
        columnBtn: false,
        menu: true,
        searchLabelWidth: 100,
        column: [
          {
            label: '销售发货单号',
            prop: 'outboundOrderNo',
            search: true,
            searchOrder: 2
          },
          {
            label: '客户',
            prop: 'customerName',
            search: true,
            searchOrder: 5
          },
          {
            label: '出库时间',
            prop: 'createTime'
          },
          {
            label: '操作人',
            prop: 'operatorName'
          }
        ]
      },
      baseOption: {
        submitBtn: false,
        emptyBtn: false,
        labelWidth: 130,
        column: [
          {
            label: '销售发货单号',
            prop: 'outboundOrderNo',
            disabled: true,
            span: 7
          },
          {
            label: '客户',
            prop: 'customerName',
            disabled: true,
            span: 7
          }
        ]
      },
      detailOption: {
        height: '500px',
        calcHeight: 30,
        tip: false,
        border: true,
        selection: false,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        menu: false,
        columnBtn: false,
        column: [
          {
            label: '产品编码',
            prop: 'productPartNo'
          },
          {
            label: '产品名称',
            prop: 'productPartName'
          },
          {
            label: '产品型号',
            prop: 'productModel'
          },
          {
            label: '工单号',
            prop: 'workOrderNo'
          },
          {
            label: '生成批次',
            prop: 'lotNo'
          },
          {
            label: '出库数量',
            prop: 'quantity'
          },
          {
            label: '仓库名称',
            prop: 'warehouseName'
          },
          {
            label: '关联销售单号',
            prop: 'salesOrderNo'
          }
        ]
      },
      detailPage: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      showDetailDialog: false,
      detailLoading: false,
      detailQuery: {},
      data: []
    };
  },
  computed: {
    ...mapGetters(['permission']),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  created() {},
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    searchReset() {
      this.query = {};
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    detailCurrentChange(currentPage) {
      this.detailPage.currentPage = currentPage;
      this.queryDetailList(this.detailPage);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    detailSizeChange(pageSize) {
      this.detailPage.pageSize = pageSize;
      this.queryDetailList(this.detailPage);
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    /**
     * 查看
     */
    handleView(row) {
      this.showDetailDialog = true;
      this.detailLoading = true;
      this.form = row;
      this.queryDetailList(this.detailPage);
    },
    queryDetailList(page, params = {}) {
      params.outboundRecordId = this.form.id;
      getDetailList(page.currentPage, page.pageSize, Object.assign(params)).then((res) => {
        const data = res.data.data;
        this.detailPage.total = data.total;
        this.detailData = data.records;
        this.detailLoading = false;
      });
    },
    detailExport() {
      this.$refs.detailExportComp.visible = true;
    },
    newKey() {
      this.timer = new Date().getTime();
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .sail-header-dialog-content {
  .el-card__body {
    .avue-crud__menu {
      display: none;
    }
  }
}
.formClass {
  margin-top: 20px;
}
.title-result {
  display: flex;
  justify-content: space-between;
  .title {
    display: flex;
    align-items: center;
  }
}
</style>
